﻿using Common;
using CSharpConnectMySQL;
using Dingding.Helper;
using MySqlApi.Models;
using MySqlX.XDevAPI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Ocsp;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Web;
using System.Web.Http;

 
  



namespace MySqlApi.Controllers
{
    [RoutePrefix("api/Pono_No")]
    public class Pono_NoController : ApiController
    {
        string ps_name = "PostPono_No_GetV1";


        /// <summary>
        /// 原始数据；[炉号钩号]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("login")]
        [AllowAnonymous]
        public IHttpActionResult Login([FromBody] JObject req)
        {

             
            var sessionId = Guid.NewGuid().ToString("N");
            var now = DateTime.UtcNow;
             

            var minutesStr = System.Configuration.ConfigurationManager.AppSettings["Auth:SessionTimeoutMinutes"];
            if (!int.TryParse(minutesStr, out var minutes)) minutes = 30;
            var ttl = TimeSpan.FromMinutes(minutes);

            // 存 Redis
           // RedisConnectionManager.SessionStore.StoreSession(session, ttl);



            // 设置 Cookie
            var cookieName = System.Configuration.ConfigurationManager.AppSettings["Auth:CookieName"] ?? "AuthSession";
            var cookieSecureStr = System.Configuration.ConfigurationManager.AppSettings["Auth:CookieSecure"];
            bool cookieSecure = bool.TryParse(cookieSecureStr, out var cs) && cs;

            var cookie = new HttpCookie(cookieName, sessionId)
            {
                HttpOnly = true,
                Secure = cookieSecure,
                Path = "/",
                Expires = DateTime.UtcNow.Add(ttl)
            };

            // 如果需要设置 Domain（跨子域 SSO），在此设置 cookie.Domain = ".yourdomain.com";
            HttpContext.Current.Response.Cookies.Add(cookie);
            return Ok(new ApiResponse { Success = true, Message = "登录成功"+ sessionId.ToString() });

        }

        public class ApiResponse
        {
            public bool Success { get; set; }
            public string Message { get; set; }
        }





    }
}